'use strict';

import HTTPUtil from './HTTPUtil'
import Global from './GlobalConst';
import Toast from '../Utils/toast';
export default class FoundAPIService {
    /**
     * 发起游记
     */
    static initiatedTravel(token, registered_id, title, content) {
        var url = Global.HOST + '/Find/initiatedTravel';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("token", token);
            formData.append("registered_id", registered_id);
            formData.append("title", title);
            formData.append("content", content);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json.result);
                } else {
                    if (json.code == 100) {
                        Toast.show('参数不能为空');
                    } else if (json.code == 105) {
                        Toast.show('账号异地登录异常！');
                    }else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * 发起有问必答
     */
    static initiatedQuestionAnswer(token, registered_id, reward, content) {
        var url = Global.HOST + '/Find/initiatedQuestionAnswer';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("token", token);
            formData.append("registered_id", registered_id);
            formData.append("reward", reward);
            formData.append("content", content);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json.result);
                } else {
                    if (json.code == 100) {
                        Toast.show('参数不能为空');
                    } else if (json.code == 105) {
                        Toast.show('账号异地登录异常！');
                    }else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * 发起自驾同游
     */
    static initiatedWithTour(token, registered_id, set_off_date, return_date, destination, departure, content) {
        var url = Global.HOST + '/Find/initiatedWithTour';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("token", token);
            formData.append("registered_id", registered_id);
            formData.append("set_off_date", set_off_date);
            formData.append("return_date", return_date);
            formData.append("destination", destination);
            formData.append("departure", departure);
            formData.append("content", content);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json.result);
                } else {
                    if (json.code == 100) {
                        Toast.show('参数不能为空');
                    } else if (json.code == 105) {
                        Toast.show('账号异地登录异常！');
                    }else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * 发起帮购
     */
    static initiatedHelpToBuy(token, registered_id, start_time, end_time, content, shop_id) {
        var url = Global.HOST + '/Find/initiatedHelpToBuy';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("token", token);
            formData.append("registered_id", registered_id);
            formData.append("start_time", start_time);
            formData.append("end_time", end_time);
            formData.append("content", content);
            formData.append("shop_id", shop_id);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json.result);
                } else {
                    if (json.code == 100) {
                        Toast.show('参数不能为空');
                    } else if (json.code == 105) {
                        Toast.show('账号异地登录异常！');
                    }else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * 图片上传（发现模块）
     */
    static updateImg(token, registered_id, did, images) {
        var url = Global.HOST + '/Find/updateImg';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("token", token);
            formData.append("registered_id", registered_id);
            formData.append("did", did);
            let file = {uri: 'file://' + images, type: 'multipart/form-data', name: 'images.jpg'};
            formData.append("images", file);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json.result);
                } else {
                    if (json.code == 100) {
                        Toast.show('参数不能为空');
                    } else if (json.code == 105) {
                        Toast.show('账号异地登录异常！');
                    }else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * 活动列表
     */
    static activityList(search, page) {
        var url = Global.HOST + '/Find/activityList';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("search", search);
            formData.append("page", page);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json);
                } else {
                    if (json.code == 100) {
                        Toast.show('暂无数据');
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * 活动详情
     */
    static activityDetail(id) {
        var url = Global.HOST + '/Find/activityDetail';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("id", id);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json.result);
                } else {
                    if (json.code == 100) {
                        Toast.show('参数不能为空');
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * 最新/热议动态
     */
    static dynamic(type, page) {
        var url = Global.HOST + '/Find/dynamic';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("type", type);
            formData.append("page", page);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json);
                } else {
                    if (json.code == 100) {
                        Toast.show('参数不能为空');
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * 搜索最新/热议动态
     */
    static searchDynamic(search, type, page) {
        var url = Global.HOST + '/Find/searchDynamic';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("search", search);
            formData.append("type", type);
            formData.append("page", page);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json);
                } else {
                    if (json.code == 100) {
                        Toast.show('暂无数据');
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }
    /**
     * 攻略列表
     */
    static raiders_list(search,page) {
        var url = Global.HOST + '/Find/raiders_list';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("search", search);
            formData.append("page", page);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == 200) {
                    resolve(json);
                } else {
                    if (json.code == 100) {
                        Toast.show('暂无数据');
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * 攻略详情
     */
    static raiders_detail(id) {
        var url = Global.HOST + '/Find/raiders_detail';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("id", id);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == 200) {
                    resolve(json);
                } else {
                    if (json.code == 100) {
                        Toast.show('参数不能为空');
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }
    /**
     * 发起攻略评论
     */
    static raiders_comment(token, registered_id, rid, content) {
        var url = Global.HOST + '/Find/raiders_comment';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("token", token);
            formData.append("registered_id", registered_id);
            formData.append("rid", rid);
            formData.append("content", content);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json.result);
                } else {
                    if (json.code == 100) {
                        Toast.show('参数不能为空');
                    }
                    if (json.code == 203) {
                        Toast.show('您已评论！');
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * 添加评论的图片
     */
    static raiders_img(token, registered_id, cid, images) {
        var url = Global.HOST + '/Find/raiders_img';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("token", token);
            formData.append("registered_id", registered_id);
            formData.append("cid", cid);
            let file = {uri: 'file://' + images, type: 'multipart/form-data', name: 'images.jpg'};
            formData.append("images", file);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json.result);
                } else {
                    if (json.code == 100) {
                        Toast.show('参数不能为空');
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }
    /**
     * 获取攻略的所有评论
     */
    static getComment(id,page) {
        var url = Global.HOST + '/Find/raiders_more_comment';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("rid", id);
            formData.append("page", page);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == 200) {
                    resolve(json);
                } else {
                    if (json.code == 100) {
                        Toast.show('暂无数据');
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }
    /**
     * 约伴详情
     */
    static datingPartnerDetails(id) {
        var url = Global.HOST + '/Find/datingPartnerDetails';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("id", id);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json.result);
                } else {
                    if (json.code == 100) {
                        Toast.show('参数不能为空');
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * 有赏问答详情
     */
    static questionAnswerDetails(id) {
        var url = Global.HOST + '/Find/questionAnswerDetails';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("id", id);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json.result);
                } else {
                    if (json.code == 100) {
                        Toast.show('参数不能为空');
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * 帮购详情
     */
    static helpToBuyDetails(id) {
        var url = Global.HOST + '/Find/helpToBuyDetails';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("id", id);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json.result);
                } else {
                    if (json.code == 100) {
                        Toast.show('参数不能为空');
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * 游记详情
     */
    static travelDetails(id) {
        var url = Global.HOST + '/Find/travelDetails';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("id", id);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json.result);
                } else {
                    if (json.code == 100) {
                        Toast.show('参数不能为空');
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * 评论
     */
    static comment(token, registered_id, id, content) {
        var url = Global.HOST + '/Find/comment';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("token", token);
            formData.append("registered_id", registered_id);
            formData.append("id", id);
            formData.append("content", content);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json.result);
                } else {
                    if (json.code == 100) {
                        Toast.show('参数不能为空');
                    }else if (json.code == 105) {
                        Toast.show('账号异地登录异常！');
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * 评论图片
     */
    static commentImg(token, registered_id, id, images) {
        var url = Global.HOST + '/Find/commentImg';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("token", token);
            formData.append("registered_id", registered_id);
            formData.append("id", id);
            let file = {uri: 'file://' + images, type: 'multipart/form-data', name: 'images.jpg'};
            formData.append("images", file);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json.result);
                } else {
                    if (json.code == 100) {
                        Toast.show('参数不能为空');
                    }else if (json.code == 105) {
                        Toast.show('账号异地登录异常！');
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }
}